/**
 * Created by zhangPeng on 2017/1/13.
 */

app.controller('sysMonitorCtrl', function ($scope, sysMonitorService, $timeout) {

    var lastRreciSize = 0;
    var lastSendSize = 0;
    var MQlastRreciSize = 0;
    var MQlastSendSize = 0;

    /**
     * js中百分数转小数
     * @param percent
     * @returns {number}
     */
    function toPoint(percent) {
        return Number(percent.replace('%', '')) / 100;
    }


    /**
     * js中小数转百分数
     * @returns {string}
     */
    Number.prototype.toPercent = function () {
        return (Math.round(this * 10000) / 100).toFixed(2) + '%';
    }

    var date = [];
    var data1 = [];
    var data2 = [];

    /**
     * GW网路数据
     * @param shift
     */
    function addData(shift) {
        var today = new Date();
        date.push(today.getHours() + ":" + today.getMinutes() + " " + today.getSeconds());
        data1.push($scope.reciSize);
        data2.push($scope.sendSize);
        if (shift) {
            date.shift();
            data1.shift();
            data2.shift();
        }
    }

    /**
     * 刷新时间
     * @param time
     */
    $scope.changeTime = function(time){
        $scope.gwTime=time;
    }

    /**
     * GW系统监控
     */
    function getGWSystemService() {
        sysMonitorService.getGWSystemService({
            action: 'getGWSystemService'
        }, function (data) {
            if (data.message != null) {
                $scope.cpuCombined1 = data.message.cpu.combined.toPercent();
                $scope.cpuCombined2 = data.message.cpu.combined;
                $scope.memUserd = data.message.mem.memUsed.toFixed(2);
                $scope.memFree = data.message.mem.memFree.toFixed(2);
                $scope.fileFree = data.message.file.fileFree.toFixed(2);
                $scope.fileUsed = data.message.file.fileUsed.toFixed(2);
                $scope.reciSize = ((data.message.net.reciSize / 1204) - lastRreciSize).toFixed(4);
                var temp = data.message.net.sendSize / 1024;
                $scope.sendSize = (temp - lastSendSize).toFixed(4);
                lastSendSize = temp;
                lastRreciSize = data.message.net.reciSize / 1204;
            } else {
                $scope.cpuCombined1 = '0.00%';
                $scope.cpuCombined2 = 0;
                $scope.memUserd = 0;
                $scope.memFree = 0;
                $scope.fileFree = 0;
                $scope.fileUsed = 0;
                $scope.reciSize = 0;
                $scope.sendSize = 0;
            }

            // 基于准备好的dom，初始化echarts图表
            var myChart1 = echarts.init(document.getElementById('main1'));
            var myChart2 = echarts.init(document.getElementById('main2'));
            var myChart3 = echarts.init(document.getElementById('main3'));
            var myChart4 = echarts.init(document.getElementById('main4'));
            addData();
            addData(true);
            var option = {
                title: {
                    text: '网络监控',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder'
                    }
                },
                //设置图表大小
                grid: {
                    x: 115,
                    y: 60
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: date
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        //animation: false
                    }
                },
                yAxis: {
                    boundaryGap: [0, '80%'],
                    axisLabel: {
                        formatter: '{value}（KB）'
                    },
                    splitNumber: 5
                },
                series: [

                    {
                        name: '接收',
                        type: 'line',
                        showSymbol: false,
                        hoverAnimation: false,
                        data: data1
                    },
                    {
                        name: '发送',
                        type: 'line',
                        showSymbol: false,
                        hoverAnimation: false,
                        data: data2
                    }
                ]
            };
            var option1 = {
                title: {
                    text: 'CPU',
                    //subtext: '纯属虚构',
                    x: '39%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c}%"
                },
                toolbox: {
                    feature: {}
                },
                series: [
                    {
                        name: 'CPU指标',
                        type: 'gauge',
                        //$scope.cpuCombined1,
                        //            $scope.cpuIdle1 = data.message.cpu.idle;
                        //$scope.cpuCombined1 = data.message.cpu.combined.toPercent();
                        //占用比例样式
                        detail: {
                            formatter: $scope.cpuCombined1,
                            textStyle: {
                                color: 'auto',
                                fontSize: 15
                            }
                        },
                        data: [{
                            value: Math.round($scope.cpuCombined2 * 10000) / 100,
                            name: '占用率'
                        }
                        ],
                        //坐标标签样式
                        axisLabel: {
                            show: false,
                            formatter: null,
                            textStyle: {
                                color: 'auto'
                            }
                        },

                        //指针
                        pointer: {
                            length: '70%',
                            width: 4,
                            color: 'auto'
                        }
                    }
                ]
            };
            var option2 = {
                title: {
                    text: '内存',
                    //subtext: '纯属虚构',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{d}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: [{
                        value: $scope.memUserd,
                        name: '已用M:' + $scope.memUserd
                    },
                        {

                            value: $scope.memFree,
                            name: '空闲M:' + $scope.memFree
                        }]
                },
                series: [
                    {
                        name: '内存容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            {
                                value: $scope.memUserd,
                                name: '已用M:' + $scope.memUserd
                            },
                            {

                                value: $scope.memFree,
                                name: '空闲M:' + $scope.memFree
                            }
                        ]
                    }
                ]
            };
            var option3 = {
                title: {
                    text: '硬盘',
                    //subtext: '纯属虚构',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{d}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: [{
                        value: $scope.fileUsed,
                        name: '已用G:' + $scope.fileUsed
                    },
                        {

                            value: $scope.fileFree,
                            name: '空闲G:' + $scope.fileFree
                        }]
                },
                series: [
                    {
                        name: '硬盘容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            {
                                value: $scope.fileUsed,
                                name: '已用G:' + $scope.fileUsed
                            },
                            {

                                value: $scope.fileFree,
                                name: '空闲G:' + $scope.fileFree
                            }
                        ]
                    }
                ]
            };
            // 为echarts对象加载数据
            myChart1.setOption(option1);
            myChart2.setOption(option2);
            myChart3.setOption(option3);
            if (option && typeof option === "object") {
                myChart4.setOption(option, true);
            }
            //}

            //else {
            //    bootbox.alert("查询失败(" + data.reason + ")");
            //}
        });
        $timeout(function () {
            getGWSystemService();
        }, $scope.gwTime);
    };

    var MQdate = [];
    var MQdata1 = [];
    var MQdata2 = [];

    /**
     * MQ网路数据
     * @param shift
     */
    function addMQData(shift) {
        var today = new Date();
        MQdate.push(today.getHours() + ":" + today.getMinutes() + " " + today.getSeconds());
        MQdata1.push($scope.MQreciSize);
        MQdata2.push($scope.MQsendSize);
        if (shift) {
            MQdate.shift();
            MQdata1.shift();
            MQdata2.shift();
        }
    }

    /**
     * MQ系统监控
     */
    function getMQSystemService() {
        sysMonitorService.getMQSystemService({
            action: 'getMQSystemService'
        }, function (data) {
            if (data.message != null) {
                $scope.MQcpuCombined1 = data.message.cpu.combined.toPercent();
                $scope.MQcpuCombined2 = data.message.cpu.combined;
                $scope.MQmemUserd = data.message.mem.memUsed.toFixed(2);
                $scope.MQmemFree = data.message.mem.memFree.toFixed(2);
                $scope.MQfileFree = data.message.file.fileFree.toFixed(2);
                $scope.MQfileUsed = data.message.file.fileUsed.toFixed(2);
                $scope.MQreciSize = ((data.message.net.reciSize / 1204) - MQlastRreciSize).toFixed(4);
                var temp = data.message.net.sendSize / 1024;
                $scope.MQsendSize = (temp - MQlastSendSize).toFixed(4);
                MQlastSendSize = temp;
                MQlastRreciSize = data.message.net.reciSize / 1204;
            } else {
                $scope.MQcpuCombined1 = '0.00%';
                $scope.MQcpuCombined2 = 0;
                $scope.MQmemUserd = 0;
                $scope.MQmemFree = 0;
                $scope.MQfileFree = 0;
                $scope.MQfileUsed = 0;
                $scope.MQreciSize = 0;
                $scope.MQsendSize = 0;
            }

            // 基于准备好的dom，初始化echarts图表
            var myChart1 = echarts.init(document.getElementById('MQmain1'));
            var myChart2 = echarts.init(document.getElementById('MQmain2'));
            var myChart3 = echarts.init(document.getElementById('MQmain3'));
            var myChart4 = echarts.init(document.getElementById('MQmain4'));
            addMQData();
            addMQData(true);
            var option = {
                title: {
                    text: '网络监控',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder'
                    }
                },
                //设置图表大小
                grid: {
                    x: 115,
                    y: 60
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: date
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        //animation: false
                    }
                },
                yAxis: {
                    boundaryGap: [0, '80%'],
                    axisLabel: {
                        formatter: '{value}（KB）'
                    },
                    splitNumber: 5
                },
                series: [

                    {
                        name: '接收',
                        type: 'line',
                        showSymbol: false,
                        hoverAnimation: false,
                        data: MQdata1
                    },
                    {
                        name: '发送',
                        type: 'line',
                        showSymbol: false,
                        hoverAnimation: false,
                        data: MQdata2
                    }
                ]
            };
            var option1 = {
                title: {
                    text: 'CPU',
                    //subtext: '纯属虚构',
                    x: '39%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c}%"
                },
                toolbox: {
                    feature: {}
                },
                series: [
                    {
                        name: 'CPU指标',
                        type: 'gauge',
                        //占用比例样式
                        detail: {
                            formatter: $scope.MQcpuCombined1,
                            textStyle: {
                                color: 'auto',
                                fontSize: 15
                            }
                        },
                        data: [{
                            value: Math.round($scope.MQcpuCombined2 * 10000) / 100,
                            name: '占用率'
                        }
                        ],
                        //坐标标签样式
                        axisLabel: {
                            show: false,
                            formatter: null,
                            textStyle: {
                                color: 'auto'
                            }
                        },

                        //指针
                        pointer: {
                            length: '70%',
                            width: 4,
                            color: 'auto'
                        }
                    }
                ]
            };
            var option2 = {
                title: {
                    text: '内存',
                    //subtext: '纯属虚构',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{d}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: [{
                        value: $scope.MQmemUserd,
                        name: '已用M:' + $scope.MQmemUserd
                    },
                        {

                            value: $scope.MQmemFree,
                            name: '空闲M:' + $scope.MQmemFree
                        }]
                },
                series: [
                    {
                        name: '内存容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            {
                                value: $scope.MQmemUserd,
                                name: '已用M:' + $scope.MQmemUserd
                            },
                            {

                                value: $scope.MQmemFree,
                                name: '空闲M:' + $scope.MQmemFree
                            }
                        ]
                    }
                ]
            };
            var option3 = {
                title: {
                    text: '硬盘',
                    //subtext: '纯属虚构',
                    x: '40%',
                    textStyle: {
                        fontSize: 18,
                        fontWeight: 'bolder',
                        color: '#4F4F4F'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} :{d}%"
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    y: 'bottom',
                    data: [{
                        value: $scope.MQfileUsed,
                        name: '已用G:' + $scope.MQfileUsed
                    },
                        {

                            value: $scope.MQfileFree,
                            name: '空闲G:' + $scope.MQfileFree
                        }]
                },
                series: [
                    {
                        name: '硬盘容量',
                        type: 'pie',
                        radius: ['50%', '70%'],
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: false,
                                position: 'center'
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    fontSize: '30',
                                    fontWeight: 'bold'
                                }
                            }
                        },
                        labelLine: {
                            normal: {
                                show: false
                            }
                        },
                        data: [
                            {
                                value: $scope.MQfileUsed,
                                name: '已用G:' + $scope.MQfileUsed
                            },
                            {

                                value: $scope.MQfileFree,
                                name: '空闲G:' + $scope.MQfileFree
                            }
                        ]
                    }
                ]
            };
            // 为echarts对象加载数据
            myChart1.setOption(option1);
            myChart2.setOption(option2);
            myChart3.setOption(option3);
            if (option && typeof option === "object") {
                myChart4.setOption(option, true);
            }
            //}

            //else {
            //    bootbox.alert("查询失败(" + data.reason + ")");
            //}
        });
        $timeout(function () {
            getMQSystemService();
        }, $scope.gwTime);
    };

    getGWSystemService();
    getMQSystemService();

});
